package com.ywt.test4;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.ArrayList;

public class NovelUtils {

    public static final String BASE_URL = "http://www.lewenge.cc";


    /**
     * 根据图书的地址 获取所有的章节信息
     *
     * @return
     * @throws IOException
     */
    public static ArrayList<Chapter> getChapters(String book_url) throws IOException {
        //图书的url地址
        book_url = BASE_URL + book_url;

        //请求 获取章节的html 文档对象
        Document document = Jsoup.connect(book_url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
                .timeout(5000).get();


        Element bookinfo_td = document.getElementsByClass("bookinfo_td").get(0);
        //获取到了所有章节的a标签
        Elements bookinfo_a = bookinfo_td.getElementsByTag("a");

        ArrayList<Chapter> chapters = new ArrayList();
        //遍历所有a标签
        for (Element element_a : bookinfo_a) {
            String name = element_a.text();//章节名字
            String href = element_a.attr("href");//章节地址
            System.out.println(href + "----" + name);
            chapters.add(new Chapter(name, href));
        }
        return chapters;
    }

    /**
     * 根据章节的url  下载保存到文件里
     * url http://www.lewenge.la/books/69/69364/22804714.html
     */
    public static String process_chapter(String bookName, Chapter chapter) throws IOException {
        String chapter_url = BASE_URL + chapter.getUrl();
        //请求 获取章节的html 文档对象
        Document document = Jsoup.connect(chapter_url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
                .timeout(5000).get();

        Element div_content = document.getElementById("content");
        Element p_content = div_content.getElementsByTag("p").get(0);
        String text = p_content.text();//获取文章的文本
//        System.out.println(text);
        //把所有的空格替换成 换行符"\r\n"
        text = text.replace(" ", "\r\n");

        //获取章节的标题
        Element chapter_title = document.getElementsByTag("h1").get(0);
//        System.out.println(chapter_title);
        String file_name = chapter_title.text();//取出名字作为文件名

        //存放章节的小说目录
        File dirFile = new File(bookName);
        if (!dirFile.exists()) {
            //如果文件夹不存在 就创建
            dirFile.mkdirs();
        }
        String cName = chapter.getName();
        cName = cName.replace("|", "");

        //创建写文件的 流对象
        FileWriter fw = new FileWriter(bookName + "/" + cName + ".txt");
        fw.write(file_name + "\r\n" + text);//把内容写入文件
        fw.close();//关闭文件
        return file_name + "\r\n" + text;
    }


    /**
     * 获取某个章节的详情内容
     *
     * @param chapter
     * @return
     * @throws IOException
     */
    public static String get_chapter_detail(Chapter chapter) throws IOException {
        String chapter_url = BASE_URL + chapter.getUrl();
        //请求 获取章节的html 文档对象
        Document document = Jsoup.connect(chapter_url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
                .timeout(5000).get();

        Element div_content = document.getElementById("content");
        Element p_content = div_content.getElementsByTag("p").get(0);
        String text = p_content.text();//获取文章的文本
        return text;
    }

    /**
     * 根据名字 获取所有小说信息
     * url http://www.lewenge.la/modules/article/search.php
     * post请求 传递的参数  searchkey: 小说名字
     *
     * @return
     */
    public static ArrayList<Novel> searchByName(String name) throws IOException {
        String chapter_url = BASE_URL + "/modules/article/search.php";
        //请求 获取章节的html 文档对象
        Document document = Jsoup.connect(chapter_url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36")
                .timeout(5000)
                .data("searchkey", name) //post请求的参数
                .post();//发送post请求

//        System.out.println(document);
        //获取有的图书标题的div
        Elements titleDivs = document.getElementsByClass("title");

        //存放所有小说对象的集合
        ArrayList<Novel> novels = new ArrayList<>();
        //titleDivs遍历
        for (Element titleDiv : titleDivs) {
            Element a = titleDiv.getElementsByTag("a").get(0);
            Element span = titleDiv.getElementsByTag("span").get(0);
            String bookName = a.text();//书名
            String url = a.attr("href");//书的链接地址

            String authorName = span.text();//作者名字
            System.out.println(bookName + "--" + url + "---" + authorName);

            //添加小说对象 到集合中
            novels.add(new Novel(bookName, url, authorName));
        }

        return novels;
    }

    /**
     * 读取文件内容 返回
     *
     * @param file
     * @return
     */
    public static String getDetailFromFile(File file) throws IOException {
        //创建读取文件用的FileInputStream对象
        FileInputStream fis = new FileInputStream(file);
        //读取所有的内容 注意得到的是一个字节数组
        byte[] bytes = fis.readAllBytes();
        //把字节数组 转为字符串
        String text = new String(bytes);
        return text;
    }
}
